<template>
	<view>
		<view class="content">
			<view class="title ft_36 fw-b">
				{{type==1?'登录':'注册'}}
			</view>
			<view class="con_1">
				<view class="input">
					<u-input type="number" v-model="phone" placeholder="请输入您的手机号" maxlength="11"/>
				</view>
				<view class="input display just_space">
					<u-input type="number" v-model="phonecode" placeholder="请输入验证码"/>
					<view v-if="countDown == 0" @tap="get_code" style="color: #e98217;">获取验证码</view>
					<view v-if="countDown != 0" style="color: #e98217;">{{ countDown }}s后再试</view>
				</view>
			</view>
			<view class="ft_12 display p_top_10" style="justify-content: flex-end;">
				<view class="" v-if="type==1">
					没有账号？<text style="color: #e98217;" @click="is_reg_login(2)">去注册</text>
				</view>
				<view class="" v-else>
					已有账号？<text style="color: #e98217;" @click="is_reg_login(1)">去登录</text>
				</view>
			</view>
			
			<view class="" style="padding: 50px 0;">
				<view class="loginbtn" @click="login">
					{{type==1?'登录':'注册'}}
				</view>
				<view class="p_top_10 ft_12 display">
					<u-checkbox v-model="checked" shape="circle" active-color="#e98217">
						<view class="c-999" style="font-size: 24rpx;">
							<text>我已阅读并同意</text>
							<text class="c-yellow" @click.stop="xieyi(0)">《汇彩惠品平台服务协议》、</text>
							<text class="c-yellow" @click.stop="xieyi(1)">《隐私政策》</text>
							和<text class="c-yellow" @click.stop="xieyi(2)">《法律声明》</text>
						</view>
					</u-checkbox>
					
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone:"",//手机号
				phonecode:"",//手机验证码
				countDown:"", //验证码倒计时
				
				type:1, //  1/2  登录/注册
				checked:false, //是否阅读用户协议
			}
		},
		methods: {
			is_reg_login(e){ //去登录  去注册
				this.type=e
				this.phone="" //手机号
				this.phonecode="" //手机验证码
				this.countDown=""  //验证码倒计时
			},
			xieyi(e){ //去协议
				console.log(e);
				uni.navigateTo({
					url:"../../xieyi?type="+e
				})
			},
			login(){ //登录
				if(!this.checked){
					this.$all.toast("请阅读并同意用户协议和隐私政策")
					return
				}
				this.$myRequest('user/mobilelogin',{
					mobile:this.phone,
					captcha:this.phonecode,
					type:this.type
				}).then(res=>{
					console.log(res.data);
					if(res.data.code==1){
						if(this.type==1){
							this.$all.toast("登录成功")
							this.$all.setData('user',res.data.data.userinfo)
							setTimeout(()=>{
								uni.switchTab({
									url:"../../../pages/index/index"
								})
							},800)
						}else{
							this.$all.toast("注册成功，请登录")
							
						}
					
					}else{
						this.$all.toast("登录失败")
					}
				})
				
			},
			
			get_code(){ //获取验证码
				this.$myRequest('index/sms',{
					mobile:this.phone
				}).then(res=>{
					console.log(res.data);
					this.$all.toast(res.data.msg)
					if(res.data.code==1){
						this.startDown();
					}
				})
			},
			startDown() { //验证码倒计时
				this.countDown = 60;
				let timer = setInterval(() => {
					this.countDown--;
					if (this.countDown <= 0) {
						clearInterval(timer);
					}
				}, 1000);
			},
			
			
		}
	}
</script>

<style lang="scss">
.content{
	padding: 0 25px;
	.title{
		padding: 100px 0;text-align: center;
	}
	.input{
		border-bottom: 1px solid #eeeeee;padding: 10px 0;
	}
	.loginbtn{
		padding: 15px 0;color: #FFFFFF;background-color: #e98217;text-align: center;border-radius: 5px;
	}
}

</style>
